package com.wwz.frame.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * @Description validator快速失败模式配置  快速失败即只有一个验证失败，就立即返回 false 为普通模式 ，校验全部
 * @Author wwz
 * @Date 2019/08/16
 * @Param
 * @Return
 */
@Configuration
public class ValidatorConfig {
    @Bean
    public Validator validator() {
        // 方式一：
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .addProperty("hibernate.validator.fail_fast", "false")
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();

//      方式二：
//        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
//                .configure()
//                .failFast( true )
//                .buildValidatorFactory();
//        Validator validator = validatorFactory.getValidator();

        return validator;
    }
}
